import wx
import time

class ParseGauge(wx.Dialog):
    def __init__(self, p, title):
        wx.Dialog.__init__(self, p, -1, title)

        self.MainSizer = wx.BoxSizer(orient=wx.VERTICAL)

        self.Gauge = wx.Gauge(self, -1, 50, size=(350, 30))
        self.Value = 0
        self.Label = '正在解析=>'
        self.StaticText = wx.StaticText(self, -1, self.Label)
        self.BLabel = ['取   消', '完   成']
        self.Button = wx.Button(self, -1, self.BLabel[0])

        self.MainSizer.Add(self.Gauge, 1, wx.ALL|wx.ALIGN_CENTER, 5)
        self.MainSizer.Add(self.StaticText, 0, wx.ALL|wx.ALIGN_CENTER, 5)
        self.MainSizer.Add(self.Button, 0, wx.ALL|wx.ALIGN_CENTER, 5)
        self.Gauge.SetValue(1)
        self.Bind(wx.EVT_BUTTON, self.OnCancel, self.Button)

        self.SetSizer(self.MainSizer)
        self.MainSizer.Fit(self)
        self.MainSizer.SetSizeHints(self)
    
    def SetRange(self, rangenum):
        self.Gauge.SetRange(rangenum)

    def SetValue(self, value):
        self.Gauge.SetValue(value)
        if value == self.Gauge.GetRange():
            self.Button.SetLabel(self.BLabel[1])
            self.Layout()
    
    def SetLabel(self, text, flag='part'):
        if flag == 'part':
            self.StaticText.SetLabel(self.Label + text)
        else:
            self.StaticText.SetLabel(text)
        self.Layout()

    def OnCancel(self, event):
        self.EndModal(retCode=5101)

        
if __name__ == '__main__':


    class MyFrame(wx.Frame):
        def __init__(self):
            super().__init__(None, -1, 'haha', size=(200, 100))
            btn = wx.Button(self, -1, 'baba', size=(10, 10))
            self.Bind(wx.EVT_BUTTON, self.OnClick, btn)

        def OnClick(self, event):
            dia = ParseGauge(self, 'test')
            modal = dia.ShowModal()
            dia.Destroy()

    app = wx.App()
    MyFrame().Show()
    app.MainLoop()